import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { PetEventName } from "../PetEventName";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { TilePanel } from "../../equipment/TilePanel";
import { PetLifespanRenderer } from "./PetLifespanRenderer";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { PetLsPropRenderer } from "./PetLsPropRenderer";
import { PetLifespanItemRenderer } from "./PetLifespanItemRenderer";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { PetListItemInfo } from "../info/PetListItemInfo";
import { Cache } from "../../../cache/Cache";
import { DataEvent } from "../../../events/DataEvent";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { ItemData } from "../../../resource/info/ItemData";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { MsgManager } from "../../../manager/MsgManager";
import { ItemFeatureTips } from "../../pack/social/ItemFeatureTips";
import { SPetUpdate } from "../../../../../Message/Game/SPetUpdate";
type int = number;
//class PetLifespanWindow
    
    export  class PetLifespanWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this._descItemArray = Language.getArray(54435, "value");
            setSize(425, 355);
            titleHeight = 28;
            title = Language.getString(54432);
            Dispatcher.addEventListener(PetEventName.PetLifespanRendererClick, this.onPetClickHandler);
            Dispatcher.addEventListener(PetEventName.PetLifespanPropClick, this.onPropClickHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc1=null;
            var loc2=0;
            super.createChildren();
            ObjCreate.createBg(12, 35, 160, 315, this, ImagesConst.WindowCenterA);
            ObjCreate.createBg(172, 35, 245, 315, this, ImagesConst.WindowCenterA);
            this._petListPanel = new Object /* flash.display.Sprite */();
            this._petListPanel.x = 14;
            this._petListPanel.y = 39;
            this.addChild(this._petListPanel);
            ObjCreate.createBg(2, 30, 150, 185, this._petListPanel);
            ObjCreate.createTextField(Language.getString(54433), 5, 5, 70, 20, this._petListPanel, GlobalStyle.textFormat2);
            this._petList = new TilePanel();
            this._petList.horizontalGap = 2;
            this._petList.verticalGap = 2;
            this._petList.setSize(140, 175);
            this._petList.columnWidth = 130;
            this._petList.rowHeight = 44;
            this._petList.setStyle("cellRenderer", PetLifespanRenderer);
            this._petList.move(5, 35);
            this.addChild(this._petList);
            this._petListPanel.addChild(this._petList);
            this._propPanel = new Object /* flash.display.Sprite */();
            this._propPanel.x = 175;
            this._propPanel.y = 39;
            this.addChild(this._propPanel);
            ObjCreate.createBg(2, 30, 235, 129, this._propPanel);
            ObjCreate.createTextField(Language.getString(54434), 10, 5, 70, 20, this._propPanel, new GTextFormat(FontUtil.songtiName, 12, 65280));
            this._lifespanTxt = ObjCreate.createTextField("", 80, 5, 70, 20, this._propPanel);
            this._lifespanTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            loc2 = 0;
            while (loc2 < 15) 
            {
                loc1 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                loc1.width = 38;
                loc1.height = 38;
                this._propPanel.addChild(loc1);
                loc1.x = 5 + loc2 % 5 * 41;
                loc1.y = 35 + Math.floor(loc2 / 5) * 41;
                ++loc2;
            }
            this._propList = new TilePanel();
            this._propList.horizontalGap = 3;
            this._propList.verticalGap = 0;
            this._propList.columnWidth = 38;
            this._propList.rowHeight = 41;
            this._propList.setSize(220, 123);
            this._propList.move(5, 35);
            this._propList.setStyle("cellRenderer", PetLsPropRenderer);
            this._propPanel.addChild(this._propList);
            this._tipTxt = ObjCreate.createTextField("", 17, 265, 150, 80, this);
            this._tipTxt.multiline = true;
            this._tipTxt.wordWrap = true;
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._tipTxt.htmlText = Language.getString(54436);
            this._itemDescPanel = new Object /* flash.display.Sprite */();
            this._itemDescPanel.x = 178;
            this._itemDescPanel.y = 200;
            this.addChild(this._itemDescPanel);
            ObjCreate.createBg(0, 0, 235, 145, this._itemDescPanel);
            ObjCreate.createTextField(Language.getString(54550), 30, 5, 65, 20, this._itemDescPanel, GlobalStyle.textFormat2);
            ObjCreate.createTextField(Language.getString(54551), 140, 5, 65, 20, this._itemDescPanel, GlobalStyle.textFormat2);
            this._itemList = new TilePanel();
            this._itemList.setSize(205, 120);
            this._itemList.columnWidth = 205;
            this._itemList.rowHeight = 20;
            this._itemList.setStyle("cellRenderer", PetLifespanItemRenderer);
            this._itemList.move(25, 25);
            this._itemList.dataProvider = new DataProvider(this._descItemArray);
            this._itemDescPanel.addChild(this._itemList);
            return;
        }

        public selectByUid(arg1: string, arg2: Boolean=true): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=this._petList.dataProvider.toArray();
            var loc4=0;
            while (loc4 < loc1.length) 
            {
                loc2 = (loc3 = loc1[loc4] as PetListItemInfo).petInfo;
                if (arg1 == loc2.uid) 
                {
                    if (arg2) 
                    {
                        this._petList.selectedIndex = loc4;
                        this._petList.scrollToSelected();
                    }
                    loc3.petInfo = Cache.instance.pet.getPetInfoByUid(arg1);
                    this._petList.invalidateItem(loc3);
                    break;
                }
                ++loc4;
            }
            return;
        }

        /* internal  */updatePetList(): void
        {
            var loc3=null;
            var loc1=Cache.instance.pet.petInfos;
            var loc2=[];
            var loc4=new DataProvider();
            var loc5=0;
            while (loc5 < loc1.length) 
            {
                loc3 = new PetListItemInfo();
                loc3.petInfo = loc1[loc5];
                loc3.isEnable = true;
                loc4.addItem(loc3);
                ++loc5;
            }
            this._petList.dataProvider = loc4;
            return;
        }

        /* internal  */updatePropList(): void
        {
            var loc1=Cache.instance.pack.backPackCache.getItemsByJudge(this.isSuitProp);
            var loc2=new DataProvider();
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc2.addItem(loc1[loc3]);
                ++loc3;
            }
            this._propList.dataProvider = loc2;
            return;
        }

        /* internal  */onPetClickHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetInfo;
            this._petInfo = Cache.instance.pet.getPetInfoByUid(loc1.uid);
            this._lifespanTxt.htmlText = HTMLUtil.addColor(int(loc1.lifeSpan / 5).toString(), "#f0ea3f") + HTMLUtil.addColor("/27000", "#00ff00");
            return;
        }

        /* internal  */isSuitProp(arg1: ItemData): Boolean
        {
            return ItemsUtil.isPetEgg(arg1) || ItemsUtil.isPetLifeSpan(arg1) || ItemsUtil.isCookedFish(arg1);
        }

        /* internal  */onPropClickHandler(arg1: DataEvent): void
        {
            var loc2=0;
            if (!this._petInfo) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54624));
                return;
            }
            var loc1=arg1.data as ItemData;
            if (loc1) 
            {
                if (ItemsUtil.isPetEgg(loc1) || ItemsUtil.isCookedFish(loc1)) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetEatEgg, {"uid": this._petInfo.uid, "eggs":[loc1.uid]}));
                }
                else 
                {
                    loc2 = loc1.itemAmount;
                    if (loc2 != 1) 
                    {
                        ItemFeatureTips.bulkUse(loc1, this.batchUse);
                    }
                    else 
                    {
                        this.batchUse(loc1, loc2);
                    }
                }
            }
            return;
        }

        /* internal  */batchUse(arg1: ItemData, arg2: int): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetLifespanAdd, {"petUid": this._petInfo.uid, "drugUid": arg1.uid, "drugNum": arg2}));
            return;
        }

        /* internal  */onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            if (this.isHide == false && this._petInfo) 
            {
                loc1 = arg1.data as SPetUpdate;
                loc2 = loc1.petInfo;
                if (this._petInfo.uid != loc2.uid) 
                {
                    this.selectByUid(loc2.uid, false);
                }
                else 
                {
                    this.selectByUid(loc2.uid, true);
                }
            }
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object=null): void
        {
            if (this.isHide == false) 
            {
                this.updatePropList();
            }
            return;
        }

        protected /* override */ setWindowCenter(): void
        {
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this.updatePetList();
            this.updatePropList();
            this._petList.selectedIndex = 0;
            return;
        }

        /* internal */ /* var */_petListPanel: Object /* flash.display.Sprite */;

        /* internal */ /* var */_petList: TilePanel;

        /* internal */ /* var */_propPanel: Object /* flash.display.Sprite */;

        /* internal */ /* var */_propList: TilePanel;

        /* internal */ /* var */_lifespanTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_itemDescPanel: Object /* flash.display.Sprite */;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_itemList: TilePanel;

        /* internal */ /* var */_descItemArray: Array<any>;

        /* internal */ /* var */_petInfo: SPetInfo;
    }
